// Copyright 2013 Google Inc. All Rights Reserved.

#include <errno.h>
#include <irt_syscalls.h>
#include <sys/socket.h>


int accept(int sock, struct sockaddr* address, socklen_t* len) {
  ENOSYS_IF_NULL(__nacl_irt_accept)

  struct nacl_abi_sockaddr na_address;
  size_t na_len;

  int newfd;
  int result = __nacl_irt_accept(sock, &na_address, na_len, &newfd);
  if (result != 0) {
    errno = __nacl_abi_errno_to_errno(result);
    return -1;
  }

  size_t nacl_len;
  __nacl_abi_sockaddr_to_sockaddr(&na_address, na_len, address, &nacl_len);
  *len = nacl_len;
  return newfd;
}
